define([
    "UtilDir/util",
    "UtilDir/tool",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
    "Date", "DateCN", "css!DateCss",
    "EasyUI", "EasyUI-lang"
], function (Util, Tool, SelectOrg, SearchBlock) {

    var sysPath = getServer() + "/static/app/fxpg/fxCasulatyOffline";

    var fxCasulatyOfflineFormHtml = sysPath + "/views/fxCasulatyOfflineForm.html";
    var getFxCasulatyOfflineController = function () {
        return getServer() + "/fxCasulatyOfflines";
    };

    /**
     * 页面初始化
     */
    var init = function () {
        initSearchBlock();
        queryBtnBind();
        initcreateProvince();
        initCasulatySelect();
        initPeriodSelect();
        initDamageDegreeSelect();
        createFxCasulatyOfflineGrid();
    };

    // 搜索栏省市区
    var initcreateProvince = function () {
        var html = "";
        $("#citySelect").append(html);
        $("#areaSelect").append(html);
        $.ajax({
            url: getServer() +"/divisions/root/subdivisions",
            contentType: "application/json",
            type: "get",
            success: function (data) {
                $.each(data, function (idx, item) {
                    html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                });
                $("#provinceSelect").append(html);
            }
        });
        $("#provinceSelect").change(function () {
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#citySelect option").remove();
            $("#citySelect").append(html);
            $("#areaSelect option").remove();
            $("#areaSelect").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            if ($(this).val() == "北京市" || $(this).val() == "天津市" || $(this).val() == "上海市" || $(this).val() == "重庆市") {
                $("#citySelect").append("<option value='" + $(this).val() + "' exid='" + divisionId + "'>" + $(this).val() + "</option>");
                return;
            }
            $.ajax({
                url: getServer() +"/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#citySelect").append(html);
                }
            });
        });
        $("#citySelect").change(function () {
            if ($(this).val() == "") return;
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#areaSelect option").remove();
            $("#areaSelect").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            $.ajax({
                url: getServer() +"/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#areaSelect").append(html);
                }
            });
        });
    }

    // 添加表单省市区
    var initcreateProvinceForm = function () {
        var html = "";
        $("#city").append(html);
        $("#area").append(html);
        $.ajax({
            url: getServer() +"/divisions/root/subdivisions",
            contentType: "application/json",
            type: "get",
            success: function (data) {
                $.each(data, function (idx, item) {
                    html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                });
                $("#province").append(html);
            }
        });
        $("#province").change(function () {
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#city option").remove();
            $("#city").append(html);
            $("#area option").remove();
            $("#area").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            if ($(this).val() == "北京市" || $(this).val() == "天津市" || $(this).val() == "上海市" || $(this).val() == "重庆市") {
                $("#city").append("<option value='" + $(this).val() + "' exid='" + divisionId + "'>" + $(this).val() + "</option>");
                return;
            }
            $.ajax({
                url: getServer() +"/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#city").append(html);
                }
            });
        });
        $("#city").change(function () {
            if ($(this).val() == "") return;
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#area option").remove();
            $("#area").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            $.ajax({
                url: getServer() +"/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#area").append(html);
                }
            });
        });
    }

    // 编辑表单省市区
    var editProvince = function (Province, City, Area) {
        debugger;
        var html = "";
        $("#city").append(html);
        $("#area").append(html);
        $.ajax({
            url: getServer() +"/divisions/root/subdivisions",
            contentType: "application/json",
            type: "get",
            success: function (data) {
                $.each(data, function (idx, item) {
                    if (item.divisionName == Province) {
                        debugger;
                        var divisionId = item.divisionId;
                        var htmlCity = '';
                        if (Province == "北京市" || Province == "天津市" || Province == "上海市" || Province == "重庆市") {
                            $("#city").append("<option value='" + Province + "' exid='" + divisionId + "'>" + Province + "</option>");
                            $('#city').val(City);
                            var htmlArea = '';
                            $.ajax({
                                url: getServer() +"/divisions/" + divisionId + "/subdivisions",
                                contentType: "application/json",
                                type: "get",
                                success: function (data) {
                                    $.each(data, function (idx, item) {
                                        htmlArea += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                                    });
                                    $("#area").append(htmlArea);
                                    $('#area').val(Area);
                                }
                            });

                        } else {
                            $.ajax({
                                url: getServer() +"/divisions/" + divisionId + "/subdivisions",
                                contentType: "application/json",
                                type: "get",
                                success: function (data) {
                                    debugger;
                                    $.each(data, function (idx, item) {
                                        if (item.divisionName == City) {
                                            var divisionId = item.divisionId;
                                            var htmlArea = '';
                                            $.ajax({
                                                url: getServer() +"/divisions/" + divisionId + "/subdivisions",
                                                contentType: "application/json",
                                                type: "get",
                                                success: function (data) {
                                                    $.each(data, function (idx, item) {
                                                        htmlArea += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                                                    });
                                                    $("#area").append(htmlArea);
                                                    $('#area').val(Area);
                                                }
                                            });
                                        }
                                        htmlCity += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                                    });
                                    $("#city").append(htmlCity);
                                    $('#city').val(City);
                                }
                            });
                        }
                    }
                    html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                });
                $("#province").append(html);
                $('#province').val(Province);
            }
        });
        $("#province").change(function () {
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#city option").remove();
            $("#city").append(html);
            $("#area option").remove();
            $("#area").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            if ($(this).val() == "北京市" || $(this).val() == "天津市" || $(this).val() == "上海市" || $(this).val() == "重庆市") {
                $("#city").append("<option value='" + $(this).val() + "' exid='" + divisionId + "'>" + $(this).val() + "</option>");
                return;
            }
            $.ajax({
                url: getServer() +"/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#city").append(html);
                }
            });
        });
        $("#city").change(function () {
            if ($(this).val() == "") return;
            var html = '<option value="" disabled selected style="display:none;">请选择</option>';
            $("#area option").remove();
            $("#area").append(html);
            var divisionId = $(this).find("option:selected").attr("exid");
            $.ajax({
                url: getServer() +"/divisions/" + divisionId + "/subdivisions",
                contentType: "application/json",
                type: "get",
                success: function (data) {
                    $.each(data, function (idx, item) {
                        html += "<option value='" + item.divisionName + "' exid='" + item.divisionId + "'>" + item.divisionName + "</option>";
                    });
                    $("#area").append(html);
                }
            });
        });
    }

    var initCasulatySelect = function () {
        $.ajax({
            url: getFxCasulatyOfflineController() + "/getValidDictItemsByDictCode/" + "人员伤亡",
            type: "get",
            success: function (data) {
                var mySelect = document.getElementById("casulatySelect");
                mySelect.add(new Option("请选择", ''), 0);
                for (var i = 0; i < data.length; i++) {
                    var name = data[i].dictItemName;
                    var code = data[i].dictItemCode;
                    mySelect.add(new Option(name, code));
                }
            }
        });
    };
    var initPeriodSelect = function () {
        $.ajax({
            url: getFxCasulatyOfflineController() + "/getValidDictItemsByDictCode/" + "时间段",
            type: "get",
            success: function (data) {
                var mySelect = document.getElementById("periodSelect");
                mySelect.add(new Option("请选择", ''), 0);
                for (var i = 0; i < data.length; i++) {
                    var name = data[i].dictItemName;
                    var code = data[i].dictItemCode;
                    mySelect.add(new Option(name, code));
                }
            }
        });
    };
    var initDamageDegreeSelect = function () {
        $.ajax({
            url: getFxCasulatyOfflineController() + "/getValidDictItemsByDictCode/" + "damageDegree",
            type: "get",
            success: function (data) {
                var mySelect = document.getElementById("damageDegreeSelect");
                mySelect.add(new Option("请选择", ''), 0);
                for (var i = 0; i < data.length; i++) {
                    var name = data[i].dictItemName;
                    var code = data[i].dictItemCode;
                    mySelect.add(new Option(name, code));
                }
            }
        });
    };

    var initCasulatySelectFrom = function () {
        $.ajax({
            url: getFxCasulatyOfflineController() + "/getValidDictItemsByDictCode/" + "人员伤亡",
            type: "get",
            success: function (data) {
                var mySelect = document.getElementById("casulaty");
                mySelect.add(new Option("请选择", ''), 0);
                for (var i = 0; i < data.length; i++) {
                    var name = data[i].dictItemName;
                    var code = data[i].dictItemCode;
                    mySelect.add(new Option(name, code));
                }
            }
        });
    };
    var initPeriodSelectFrom = function () {
        $.ajax({
            url: getFxCasulatyOfflineController() + "/getValidDictItemsByDictCode/" + "时间段",
            type: "get",
            success: function (data) {
                var mySelect = document.getElementById("period");
                mySelect.add(new Option("请选择", ''), 0);
                for (var i = 0; i < data.length; i++) {
                    var name = data[i].dictItemName;
                    var code = data[i].dictItemCode;
                    mySelect.add(new Option(name, code));
                }
            }
        });
    };
    var initDamageDegreeSelectFrom = function () {
        $.ajax({
            url: getFxCasulatyOfflineController() + "/getValidDictItemsByDictCode/" + "damageDegree",
            type: "get",
            success: function (data) {
                var mySelect = document.getElementById("damageDegree");
                mySelect.add(new Option("请选择", ''), 0);
                for (var i = 0; i < data.length; i++) {
                    var name = data[i].dictItemName;
                    var code = data[i].dictItemCode;
                    mySelect.add(new Option(name, code));
                }
            }
        });
    };

    var initSearchBlock = function () {
        SearchBlock.init("searchBlock");
    };
    var queryBtnBind = function () {
        $("#btnSearch").click(function () {
            createFxCasulatyOfflineGrid();
        });
        $("#btnReset").click(function () {
            $("#provinceSelect").val("");
            $("#citySelect").val("");
            $("#areaSelect").val("");
            $("#casulatySelect").val("");
            $("#periodSelect").val("");
            $("#damageDegreeSelect").val("");
        });
    };
    var createFxCasulatyOfflineGrid = function () {
        $("#fxCasulatyOfflineGrid").datagrid({
            url: getFxCasulatyOfflineController() + "/queryFxCasulatyOfflines",
            method: "GET",
            fitColumns: true,
            autoRowHeight: false,
            columns: [[
                {field: "ck", checkbox: true},
                {
                    field: 'province',
                    title: '省',
                    width: '11%',
                    align: 'center',
                },
                {
                    field: 'city',
                    title: '市',
                    width: '11%',
                    align: 'center',
                },
                {
                    field: 'area',
                    title: '区',
                    width: '11%',
                    align: 'center',
                },
                {
                    field: 'casulaty',
                    title: '人员伤亡',
                    width: '11%',
                    align: 'center',
                },
                {
                    field: 'period',
                    title: '时间段',
                    width: '11%',
                    align: 'center',
                },
                {
                    field: 'damageDegree',
                    title: '破坏程度',
                    width: '11%',
                    align: 'center',
                    formatter: function (value, rowData, rowIndex) {
                        return '<a href="#" onclick="_editFxCasulatyOffline(\'' + rowData.id + '\');"> ' + rowData.damageDegree + ' </a> '
                    }
                },
                {
                    field: 'probability',
                    title: '概率值',
                    width: '11%',
                    align: 'center',
                },
                {
                    field: 'casualties',
                    title: '伤亡数',
                    width: '11%',
                    align: 'center',
                },
                {
                    field: 'expect',
                    title: '期望值',
                    width: '11%',
                    align: 'center',
                },
            ]],
            toolbar: [{
                iconCls: 'fa fa-plus-circle',
                text: "添加",
                handler: function () {
                    addFxCasulatyOffline();
                }
            }, {
                iconCls: 'fa fa-trash-o',
                text: "删除",
                handler: function () {
                    deleteFxCasulatyOffline();
                }
            }],
            queryParams: {
                province: $("#provinceSelect").val(),
                city: $("#citySelect").val(),
                area: $("#areaSelect").val(),
                casulaty: $("#casulatySelect").val(),
                period: $("#periodSelect").val(),
                damageDegree: $("#damageDegreeSelect").val(),
            },
            pagination: true,
            pageSize: 10
        });
    };
    var formValidator = function () {
        $("#fxCasulatyOfflineForm").validate({
            rules: {
                province: {
                    required: true,
                },
                city: {
                    required: true,
                },
                area: {
                    required: true,
                },
                casulaty: {
                    required: true,
                },
                period: {
                    required: true,
                },
                damageDegree: {
                    required: true,
                },
            },
            messages: {
                province: {
                    required: "省不允许为空!",
                },
                city: {
                    required: "市不允许为空!",
                },
                area: {
                    required: "区不允许为空!",
                },
                casulaty: {
                    required: "人员伤亡不允许为空!",
                },
                period: {
                    required: "时间段不允许为空!",
                },
                damageDegree: {
                    required: "破坏程度不允许为空!",
                },
            }
        });
    };
    var getFxCasulatyOffline = function (id) {
        $.ajax({
            url: getFxCasulatyOfflineController() + "/" + id,
            type: "get",
            success: function (data) {
                editProvince(data.province, data.city, data.area);
                Tool.deserialize("fxCasulatyOfflineForm", data);
            }
        });
    };

    var addFxCasulatyOffline = function () {
        var slidebar = Util.slidebar({
            url: fxCasulatyOfflineFormHtml,
            width: "580px",
            cache: false,
            close: true,
            afterLoad: function () {
                initcreateProvinceForm();
                initCasulatySelectFrom();
                initPeriodSelectFrom();
                initDamageDegreeSelectFrom();
                formValidator();
                $("#saveBtn").on("click", function () {
                    if ($("#fxCasulatyOfflineForm").valid()) {
                        var data = Tool.serialize("fxCasulatyOfflineForm");
                        $.ajax({
                            url: getFxCasulatyOfflineController(),
                            contentType: "application/json",
                            data: JSON.stringify(data),
                            type: "post",
                            success: function (data) {
                                Util.alert(data.message);
                                slidebar.close();
                                createFxCasulatyOfflineGrid();
                            }
                        });
                    }
                });
            }
        });
    };
    window._editFxCasulatyOffline = function (fxCasulatyOfflineId) {
        var slidebar = Util.slidebar({
            url: fxCasulatyOfflineFormHtml,
            width: "580px",
            cache: false,
            close: true,
            afterLoad: function () {
                initCasulatySelectFrom();
                initPeriodSelectFrom();
                initDamageDegreeSelectFrom();
                formValidator();
                getFxCasulatyOffline(fxCasulatyOfflineId);
                $("#saveBtn").on("click", function () {
                    if ($("#fxCasulatyOfflineForm").valid()) {
                        var data = Tool.serialize("fxCasulatyOfflineForm");
                        $.ajax({
                            url: getFxCasulatyOfflineController(),
                            contentType: "application/json",
                            data: JSON.stringify(data),
                            type: "put",
                            success: function (data) {
                                Util.alert(data.message);
                                slidebar.close();
                                createFxCasulatyOfflineGrid();
                            }
                        });
                    }
                });
            }
        });
    };
    var deleteFxCasulatyOffline = function () {
        var rows = $("#fxCasulatyOfflineGrid").datagrid("getSelections");
        if (rows == null || rows.length == 0) {
            Util.alert("请选择一行数据!");
            return;
        }
        Util.confirm("是否要删除选中的数据?", function () {
            var ids = "";
            $.each(rows, function (i, row) {
                ids += row.id + ",";

            });
            ids = ids.substr(0, ids.length - 1);
            $.ajax({
                url: getFxCasulatyOfflineController(),
                data: {
                    ids: ids
                },
                type: "delete",
                success: function (data) {
                    createFxCasulatyOfflineGrid();
                }
            });
        }, function () {
            return;
        });

    };

    return {
        init: init
    };
});
